package cn.edu.hnzj.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.hnzj.pojo.Match;
import cn.edu.hnzj.pojo.Record;
import cn.edu.hnzj.service.RecordService;

/**
 * @ClassName: RecordController
 * @Description: 记录的控制层
 * @author: HHaoAn
 * @Copyright: https://group.cnblogs.com/learning/
 * @date: 2022-05-03 05:50:49
 */
@Controller
@RequestMapping("/record")
public class RecordController {
	/**
	 * @Fields service : 服务层实现类
	 * @date: 2022-05-03 05:56:39
	 */
	@Resource
	private RecordService recordService;
	@Resource
	private Record record;

	/**
	 * @MethodName: getLastRecord
	 * @Description: 返回最后一个记录对象
	 * @param record
	 * @return Record
	 * @date: 2022-06-29 09:57:23
	 */
	@RequestMapping(value = "/getLastRecord.do")
	@ResponseBody
	public Record getLastRecord(HttpSession session) {
		// TODO 获取比赛对象并判空
		Match match = (Match) session.getAttribute("match");
		if (match == null)
			return null;
		// TODO 获取最后一个记录对象
		Record lastRecord = recordService.getLastRecord(match.getId());
		// TOOD 不为空时返回
		if (lastRecord != null)
			return lastRecord;
		// TODO 为空时返回默认对象
		return record;
	}

	/**
	 * @MethodName: addRecord
	 * @Description: 保存一次记录 并更新比分数据
	 * @param Record 记录对象
	 * @return Record 记录对象
	 * @date: 2022-06-24 02:30:13
	 */
	@RequestMapping(value = "/addRecord.do")
	@ResponseBody
	public Record addRecord(@RequestBody Record record) {
		// TODO 添加记录到数据库 成功返回对象
		if (recordService.addRecordByObject(record))
			return record;
		return null;
	}

	@RequestMapping(value = "/updateRecord.do")
	@ResponseBody
	public Record updateRecord(@RequestBody Record record) {
		// TODO 将队伍的id设置为修改之前的队伍id
		record.setTeamId(recordService.updateRecordByObject(record));
		return record;
	}

	@RequestMapping(value = "/getOneRecord.do")
	@ResponseBody
	public Record getRecordByMatIdSinIn(@RequestBody Record record) {
		return recordService.getRecordByMatIdSinIn(record);
	}
}
